package Main;

import java.util.HashSet;

public class Demo1 {

    public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for(int i = 0; i < nums.length;i++) {
            boolean fly = set.add(nums[i]);
            if(fly == false) {
                return true;
            }
        }

        return false;
    }

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashSet<Integer> set = new HashSet<>();
        for(int i = 0; i < nums.length; i++) {
            boolean fly = set.add(nums[i]);
            if(fly == false) {
                for(int j =0; j < i;j++) {
                    if(nums[j] == nums[i]) {
                        if(Math.abs(i - j) <= k) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

}


